package com.three.DAO.impl;

import com.three.DAO.BaseDAO;
import com.three.DAO.MobileCardDAO;
import com.three.entity.*;
import com.three.service.Common;

import java.sql.ResultSet;
import java.sql.SQLException;

public class MobileCardDAOImpl extends BaseDAO implements MobileCardDAO {

    ResultSet rs=null;

    @Override
    public boolean isValid(MobileCard mobileCard){
        String sql="select cardNumber,passWord from mobilecard where cardNumber=? and passWord=?";
        Object[] values={mobileCard.getCardNumber(),mobileCard.getPassWord()};
        boolean flag=false;
        rs=super.exeQuery(sql,values);
        try{
            while(rs.next()){
                flag=true;
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            try{
                if(rs!=null){
                    rs.close();
                }
            }catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return flag;
    }
    @Override
    public MobileCard selectMobileCard(MobileCard mobileCard){
        String sql="select * from mobilecard where cardNumber=?";
        Object[] values={mobileCard.getCardNumber()};
        rs=super.exeQuery(sql,values);
        try{
            while(rs.next()){
                mobileCard.setId(rs.getInt("id"));
                mobileCard.setUserName(rs.getString("userName"));
                mobileCard.setPassWord(rs.getString("passWord"));
                switch(rs.getInt("packageId")){
                    case 1:mobileCard.setServicePackage(Common.talkpackage);break;
                    case 2:mobileCard.setServicePackage(Common.netpackage);break;
                    case 3:mobileCard.setServicePackage(Common.superpackage);break;
                }
                mobileCard.setConsumAmount(rs.getInt("consumAmount"));
                mobileCard.setMoney(rs.getDouble("money"));
                mobileCard.setRealTalkTime(rs.getInt("realTalkTime"));
                mobileCard.setRealSMSCount(rs.getInt("realSMSCount"));
                mobileCard.setRealFlow(rs.getInt("realFlow"));

            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            try{
                if(rs!=null){
                    rs.close();
                }
            }catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return mobileCard;
    }

    @Override
    public int register(MobileCard mobileCard) {
        String sql="insert mobilecard (cardNumber,userName,passWord,packageId,money)values(?,?,?,?,?)";
        Object[] values={mobileCard.getCardNumber(),mobileCard.getUserName(),mobileCard.getPassWord(),mobileCard.getServicePackage().getId(),mobileCard.getMoney()};
        int count=super.exeUpdate(sql,values);
        return count;
    }

    @Override
    public boolean updateMobileCard(MobileCard mobileCard) {
        String sql="update mobilecard set passWord=?,packageId=?,consumAmount=?,money=?,realTalkTime=?,realSMSCount=?,realFlow=? where cardNumber=?";
        Object[] values=null;
        boolean flag=false;
        if(mobileCard.getServicePackage() instanceof TalkPackage){
            values= new Object[]{mobileCard.getPassWord(),1,mobileCard.getConsumAmount(),mobileCard.getMoney(),mobileCard.getRealTalkTime(),mobileCard.getRealSMSCount(),mobileCard.getRealFlow(), mobileCard.getCardNumber()};
        }else if(mobileCard.getServicePackage() instanceof NetPackage){
            values= new Object[]{mobileCard.getPassWord(),2,mobileCard.getConsumAmount(),mobileCard.getMoney(),mobileCard.getRealTalkTime(),mobileCard.getRealSMSCount(),mobileCard.getRealFlow(), mobileCard.getCardNumber()};
        }else if(mobileCard.getServicePackage() instanceof SuperPackage){
            values= new Object[]{mobileCard.getPassWord(),3,mobileCard.getConsumAmount(),mobileCard.getMoney(),mobileCard.getRealTalkTime(),mobileCard.getRealSMSCount(),mobileCard.getRealFlow(), mobileCard.getCardNumber()};
        }
        flag=true;
        super.exeUpdate(sql,values);
        return flag;
    }


    @Override
    public int deleteMobileCard(MobileCard mobileCard) {
        String sql="delete from mobilecard where cardNumber=?";
        Object[] values={mobileCard.getCardNumber()};
        int count=super.exeUpdate(sql,values);
        return count;
    }


}
